<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="测量Id" prop="measureId">
        <el-input v-model="dataForm.measureId" placeholder="测量Id"></el-input>
      </el-form-item>
      <el-form-item label="监测点" prop="measurePoint">
        <el-input
          v-model="dataForm.measurePoint"
          placeholder="监测点"
        ></el-input>
      </el-form-item>
      <el-form-item label="监测段" prop="partId">
        <el-input v-model="dataForm.partId" placeholder="监测段"></el-input>
      </el-form-item>
      <el-form-item label="光栅ID" prop="fbgId">
        <el-input v-model="dataForm.fbgId" placeholder="具体哪个fbg"></el-input>
      </el-form-item>
      <el-form-item label="每个fbg对应的深度" prop="deep">
        <el-input
          v-model="dataForm.deep"
          placeholder="每个fbg对应的深度"
        ></el-input>
      </el-form-item>
      <el-form-item label="每个fbg的位移" prop="displacement">
        <el-input
          v-model="dataForm.displacement"
          placeholder="每个fbg的位移"
        ></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="gmtCreate">
        <el-input
          v-model="dataForm.gmtCreate"
          placeholder="创建时间"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        measureId: "",
        measurePoint: "",
        partId: "",
        fbgId: "",
        deep: "",
        displacement: "",
        gmtCreate: ""
      },
      dataRule: {
        measureId: [
          {
            required: true,
            message: "测量id，64个为一组，硬件端自增不能为空",
            trigger: "blur"
          }
        ],
        measurePoint: [
          { required: true, message: "不同的桩不能为空", trigger: "blur" }
        ],
        partId: [
          { required: true, message: "该桩下的那一段不能为空", trigger: "blur" }
        ],
        fbgId: [
          { required: true, message: "具体哪个fbg不能为空", trigger: "blur" }
        ],
        deep: [
          {
            required: true,
            message: "每个fbg对应的深度不能为空",
            trigger: "blur"
          }
        ],
        displacement: [
          { required: true, message: "每个fbg的位移不能为空", trigger: "blur" }
        ],
        gmtCreate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/displacementnew/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.measureId = data.displacementNew.measureId;
              this.dataForm.measurePoint = data.displacementNew.measurePoint;
              this.dataForm.partId = data.displacementNew.partId;
              this.dataForm.fbgId = data.displacementNew.fbgId;
              this.dataForm.deep = data.displacementNew.deep;
              this.dataForm.displacement = data.displacementNew.displacement;
              this.dataForm.gmtCreate = data.displacementNew.gmtCreate;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/displacementnew/${
                !this.dataForm.id ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              measureId: this.dataForm.measureId,
              measurePoint: this.dataForm.measurePoint,
              partId: this.dataForm.partId,
              fbgId: this.dataForm.fbgId,
              deep: this.dataForm.deep,
              displacement: this.dataForm.displacement,
              gmtCreate: this.dataForm.gmtCreate
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
